/**
 * Filename: PreviewActivity.java
 *
 * @author Firefly
 * @contact postfirefly@163.com
 * @time 2016/3/17 15:31
 */
package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.BaseApplication;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.PreviewAdapter;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.ISkinUpdate;
import com.github.postsanf.yinian.constants.SelectedImg;
import com.github.postsanf.yinian.utils.SkinPackageManager;
import com.umeng.analytics.MobclickAgent;

import java.util.List;

/**
 * 预览选中图片
 */
public class PreviewActivity extends BaseActivity implements View.OnClickListener, ISkinUpdate {
    private Toolbar mToolbar;
    private ViewPager vp_preview_image;                       // 整个切换视图
    private TextView btn_preview_image_sure;                 // 上传
    private RelativeLayout rl_preview_bottom;
    private PreviewAdapter mAdapter;                               // 图片适配器
    private List<String> picUrlsList;       // 图片数据

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerBoradcastReceiver();
        setContentView(R.layout.yn_preview_image);
        BaseApplication.getInstance().mActivitys.add(this);
        initData();
        initView();
        setData();
    }

    private void initData() {
        picUrlsList = SelectedImg.mSelectedImage;
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.preview_toolbar);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationIcon(R.drawable.ic_return);
        rl_preview_bottom = (RelativeLayout) findViewById(R.id.rl_preview_bottom);
        vp_preview_image = (ViewPager) findViewById(R.id.vp_preview_image);
        btn_preview_image_sure = (TextView) findViewById(R.id.btn_preview_image_sure);
        updateSize();
        btn_preview_image_sure.setOnClickListener(this);
    }

    public void updateSize() {
        if (SelectedImg.mSelectedImage.size() > 0) {
            btn_preview_image_sure.setText("上传(" + SelectedImg.mSelectedImage.size() + ")");
        } else {
            btn_preview_image_sure.setText("上传");
        }
    }

    private void setData() {
        mAdapter = new PreviewAdapter(this, picUrlsList);
        vp_preview_image.setAdapter(mAdapter);
        final int size = picUrlsList.size();
        if (size > 1) {
            mToolbar.setTitle(1 + "/" + size);
        } else {
            mToolbar.setTitle("预览");
        }

        vp_preview_image.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int arg0) {
                int index = arg0 % size;
                mToolbar.setTitle((index + 1) + "/" + size);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
            }
        });
        vp_preview_image.setCurrentItem(0);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                Intent mIntent = new Intent(ACTIONs.actionUpdateUpNum);
                sendBroadcast(mIntent);
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_preview_image_sure:
//               intent2Activity(PublishStatusActivity.class);
//                Intent intent = new Intent(this, PublishActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//                startActivity(intent);
//                finish();
                // 附带数据
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent mIntent = new Intent(ACTIONs.actionUpdateUpNum);
        sendBroadcast(mIntent);
    }

    // 广播接收
    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionCloseAlbum:
                    PreviewActivity.this.finish();
                    break;
            }
        }
    };

    //注册广播
    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionCloseAlbum);
        this.registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    protected void onDestroy() {
        BaseApplication.getInstance().mActivitys.remove(this);
        this.unregisterReceiver(mBroadcastReceiver);
        super.onDestroy();
    }

    @Override
    public void updateTheme() {
        if (mToolbar != null) {
            try {
                Resources mResource = SkinPackageManager.getInstance(this).mResources;
                mToolbar.setNavigationIcon(mResource.getDrawable(mResource.getIdentifier("ic_return", CommonConstants.THEME_DRAWABLE, CommonConstants.THEME_PACKAGE)));
                mToolbar.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
                mToolbar.setTitleTextColor(mResource.getColor(mResource.getIdentifier("border_color", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
                rl_preview_bottom.setBackgroundColor(mResource.getColor(mResource.getIdentifier("yncolor", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
                btn_preview_image_sure.setTextColor(mResource.getColor(mResource.getIdentifier("border_color", CommonConstants.THEME_COLOR, CommonConstants.THEME_PACKAGE)));
                mAdapter.setResource(mResource);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        if (SkinPackageManager.getInstance(this).mResources != null) {
            updateTheme();
        }
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
